package com.sise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.entity.ProductBrowseRecord;
import com.sise.mapper.ProductBrowseRecordMapper;
import com.sise.service.IProductBrowseRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zj
 */
@Service
public class ProductBrowseRecordServiceImpl extends ServiceImpl<ProductBrowseRecordMapper, ProductBrowseRecord> implements IProductBrowseRecordService {

    @Resource
    private ProductBrowseRecordMapper productBrowseRecordMapper;

    @Override
    public boolean addOrUpdateProductBrowseRecord(String openId, Integer productId) {
        QueryWrapper<ProductBrowseRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("open_id", openId)
                .eq("product_id", productId);
        ProductBrowseRecord productBrowseRecord = productBrowseRecordMapper.selectOne(queryWrapper);

        if (productBrowseRecord != null) {
            productBrowseRecord.setBrowseCount(productBrowseRecord.getBrowseCount() + 1);
            int tag = productBrowseRecordMapper.updateById(productBrowseRecord);
            return tag == 1;
        } else {
            productBrowseRecord = new ProductBrowseRecord();
            productBrowseRecord.setOpenId(openId);
            productBrowseRecord.setProductId(productId);
            productBrowseRecord.setBrowseCount(1);
            int tag = productBrowseRecordMapper.insert(productBrowseRecord);
            return tag == 1;
        }
    }

}
